<?php
  header ("Content-type: image/png");
  header ("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
  header ("Pragma: no-cache");
  putenv ('GDFONTPATH=' . realpath('.'));
  
  require_once('../config.php');
  require_once('./funcs_img.php');
  
  $conn = @mysql_connect(DBHOST.':'.DBPORT, DBUSER, DBPASS);
  if(!$conn) empty_image('SQL Connection Error ...');
  if(!@mysql_select_db(DATABASE)) empty_image('SQL Database Error ...');
  @mysql_query('SELECT * FROM `'.TAB_SRC.'` LIMIT 1;') or empty_image('SQL Table Error ...');
  
  
  $data_str = rawurldecode($_GET['data']);
  $data_tab = explode('||', $data_str);
  for($i=0; $i<count($data_tab); $i++) {
    $data_tab[$i] = explode('|', $data_tab[$i]);
  }
  $data_tab[0][1] = intval($data_tab[0][1]);
  $data_tab[0][2] = intval($data_tab[0][2]);
  if(time() > ($data_tab[0][2]+3600))
    empty_image('Image preview is outdated ...');
  
  $sql_return = @mysql_query('SELECT * FROM `'.TAB_SRC.'` WHERE `id`='.intval($data_tab[0][0]).' AND `type`="SIGNATURE" LIMIT 1');
  $sql_data = @mysql_fetch_assoc($sql_return);
  
  if($sql_data['id'] != null) {
    $live_prev = load_img('./../'.$sql_data['path']);
    
    if((!$live_prev) || (!check_parameters_db_src($sql_data)) || (!check_parameters_arr($data_tab)) || ($data_tab[0][1] > $sql_data['fields']))
      empty_image();
    
    for($i=1; $i<=$data_tab[0][1]; $i++) {
      if($data_tab[$i][1] != $lastfont) {
        $ret = @mysql_query('SELECT `id`,`path` FROM `'.TAB_SRC.'` WHERE `id`='.intval($data_tab[$i][1]).' AND `type`="FONT" LIMIT 1');
        $dat = @mysql_fetch_assoc($ret);
        $lastfont = $dat['id'];
      }
      
      if($data_tab[$i][3] != 'true') {
        puttext($live_prev, $sql_data['f'.$i.'_x'], $sql_data['f'.$i.'_y'], $sql_data['f'.$i.'_s'], $data_tab[$i][2], './../'.$dat['path'], $data_tab[$i][0]);
      } else {
        puttextshadow($live_prev, $sql_data['f'.$i.'_x'], $sql_data['f'.$i.'_y'], $data_tab[$i][5], $data_tab[$i][6], $sql_data['f'.$i.'_s'], $data_tab[$i][2], $data_tab[$i][4], './../'.$dat['path'], $data_tab[$i][0]);
      }
    }
    
    imagepng($live_prev);
    imagedestroy($live_prev);
  } 
  else
    empty_image();  

  @mysql_close($conn);
?>